//
//  ViewController.m
//  CircleProgress
//
//  Created by 邓岚锋 on 15/9/9.
//  Copyright (c) 2015年 邓岚锋. All rights reserved.
//

#import "ViewController.h"
#import "CircleProgressLayer.h"

@interface ViewController ()<UITextFieldDelegate>{
    CircleProgressLayer *layer;
    UILabel *label;
    UILabel *HadWalkLabel;  //已经行走了
    UILabel *goalLabel;    //目标步伐
//    float    pacent;  //完成度
    UIButton *button;
}
@property(assign)float pacent;
@property(nonatomic,strong)UITextField *hadWalkTextField;
@property(nonatomic,strong)UITextField *goalTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
 
    layer = [[CircleProgressLayer alloc]init];
    layer.frame = CGRectMake(SCREEN_WIDTH/2-LAYER_BOUDS/2, 100, LAYER_BOUDS, LAYER_BOUDS);
    layer.backgroundColor = [UIColor clearColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    
    _goalTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 410, (SCREEN_WIDTH-80)/2, 30)];
    _goalTextField.backgroundColor = [UIColor orangeColor];
    _goalTextField.layer.cornerRadius = 5;
    _goalTextField.placeholder = @" 目标步数";
    _goalTextField.delegate = self;
    [self.view addSubview:_goalTextField];
    
    _hadWalkTextField = [[UITextField alloc]initWithFrame:CGRectMake( (SCREEN_WIDTH-80)/2+50, 410, (SCREEN_WIDTH-80)/2, 30)];
    _hadWalkTextField.backgroundColor = [UIColor orangeColor];
    _hadWalkTextField.layer.cornerRadius = 5;
    _hadWalkTextField.placeholder = @" 已行走步数";
    _hadWalkTextField.delegate = self;
    [self.view addSubview:_hadWalkTextField];

    HadWalkLabel = [[UILabel alloc]init];
    HadWalkLabel.frame = CGRectMake(SCREEN_WIDTH/2-50, 125+LAYER_BOUDS/3, 100, 35);
    HadWalkLabel.textAlignment = NSTextAlignmentCenter;
    HadWalkLabel.textColor = [UIColor blackColor];
    HadWalkLabel.font = [UIFont systemFontOfSize:43];
    [self.view addSubview:HadWalkLabel];
    HadWalkLabel.hidden  = YES;
    
    goalLabel = [[UILabel alloc]init];
    goalLabel.frame = CGRectMake(SCREEN_WIDTH/2-50, 155+LAYER_BOUDS/3, 100, 50);
    goalLabel.textAlignment = NSTextAlignmentCenter;
    goalLabel.textColor = [UIColor grayColor];
    goalLabel.font = [UIFont systemFontOfSize:18];
    goalLabel.hidden = YES;
    [self.view addSubview:goalLabel];

    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(30, 460, SCREEN_WIDTH-60, 40);
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"开始计算" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
   button.enabled = NO;
    [self.view addSubview:button];
    // Do any additional setup after loading the view, typically from a nib.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _hadWalkTextField) {
        HadWalkLabel.text = _hadWalkTextField.text;
    }else{
        goalLabel.text = [NSString stringWithFormat:@"目标 %@",_goalTextField.text];
    }
    if ( (_hadWalkTextField.text.length!=0) && (_hadWalkTextField.text.length!=0)) {
        _pacent = [_hadWalkTextField.text floatValue]/[_goalTextField.text floatValue];
        button.enabled = YES;
    }
    return YES;
}
-(void)move:(UITapGestureRecognizer*)tap{
    
    //创建label
    HadWalkLabel.hidden = NO;
    goalLabel.hidden  = NO;
    label = [[UILabel alloc]init];
    label.frame = CGRectMake(SCREEN_WIDTH/2-25, 85+LAYER_BOUDS/3, 50, 30);
    label.text = @"步数";
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:label];
    
    
    [CATransaction begin];
    layer.progress = _pacent; //最终暂停的百分比
    [CATransaction setDisableActions:YES];
    CABasicAnimation  *basicAnimation = [CABasicAnimation animationWithKeyPath:@"progress"];
    basicAnimation.fromValue = [ NSNumber numberWithFloat: [(CircleProgressLayer*)[layer presentationLayer] progress]];
    basicAnimation.duration = 2.0;
    basicAnimation.toValue = [NSNumber numberWithFloat:_pacent];
    [layer addAnimation:basicAnimation forKey:@"start"];
    [CATransaction commit];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
